/**
  * The framework for OVGSpriteBatch testing
  */

#include <QDebug>
#include <QImage>
#include "vgwidget.h"
#include <VG/openvg.h>



/** Loads a PNG file and creates VGImage using the 32bit image data.
  */
VGImage loadVGImage(QString filename)
{
    QImage texture;
    if(!texture.load(filename))
    {
        qDebug("loadVGImage cannot load '%s'", filename);
        return VG_INVALID_HANDLE;
    }

    int w=texture.width();
    int h=texture.height();
    if (w<1 || h<1) return VG_INVALID_HANDLE;

    VGImage image = vgCreateImage(VG_sARGB_8888, w, h, VG_IMAGE_QUALITY_FASTER);
    if(image==VG_INVALID_HANDLE)
    {
        VGErrorCode err = vgGetError();
        qDebug("loadVGImage error 0x%x", err);
    }

    vgImageSubData(image, (QRgb*)texture.bits(), w*4, VG_sARGB_8888, 0, 0, w, h);
    return image;
}



VGWidget::VGWidget(QWidget *parent)
    : QWidget(parent)
{
    vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);
    vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_BETTER);

    systemInitialized = false;
    t0 = QTime::currentTime();
    tt = t0;

    texture = VG_INVALID_HANDLE;
    spriteBatch = 0;

    setAttribute( Qt::WA_AcceptTouchEvents, true );
}


VGWidget::~VGWidget()
{
    if (texture!=VG_INVALID_HANDLE) {
        vgDestroyImage( texture ); texture = VG_INVALID_HANDLE;
    }
    if (spriteBatch) delete spriteBatch;
    spriteBatch = 0;
}

bool VGWidget::event(QEvent *event)
{
    switch(event->type())
    {
    case QEvent::TouchBegin:

        {
            QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
            foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints)
            {
                QPoint p = touchPoint.pos().toPoint();
                //game_event(touchPoint.state(), p.x(), p.y(), touchPoint.id());
            }
        }
        break;
    case QEvent::WindowActivate:
        qDebug("WindowActivate");
        setUpdatesEnabled(true);
        repaint(); // TODO update() didnt work here, investigate
        break;
    case QEvent::WindowDeactivate:
        qDebug("WindowDeactivate");
        setUpdatesEnabled(false);
        break;
    }

    QWidget::event(event);
}

void VGWidget::paintEvent(QPaintEvent *)
{
    QTime now = QTime::currentTime();
    int dt=tt.msecsTo(now);
    tt=now;

    if (systemInitialized == false)
    {
        texture = loadVGImage( QString(":/graphics/sprite1.png") );
        spriteBatch = new OVGSpriteBatch(width(), height());
        systemInitialized = true;
    }

    tester.run((float)dt/1000.0f);


    QPainter painter;
    painter.begin(this);
    painter.setRenderHints(QPainter::Antialiasing);


    if (painter.paintEngine()->type() == QPaintEngine::OpenVG)
    {
        painter.beginNativePainting();

        // clear background using black
        VGfloat color[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
        vgSetfv( VG_CLEAR_COLOR, 4, color );
        vgClear( 0, 0, width(), height());


        spriteBatch->begin();
        tester.draw( spriteBatch, texture );

        /*
            // Quick manual transform test
        SpriteDrawInfo sdi;
        sdi.textureHandle = texture;
        sdi.setTargetPos(200, 200);
        sdi.setScale(100,100);
        sdi.manualTransform(100.0f, 0.0f, 0.0f, 100.0f);
        spriteBatch->draw( &sdi );
        */

        spriteBatch->end();
        painter.endNativePainting();
    }
    else
    {
        qDebug("paintEvent skipped, not using OpenVG");
    }

#ifndef NDEBUG
    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 10));
    painter.drawText(QPointF(10, 30), message);
#endif
    painter.end();
    update();
}
